from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
from btnTools import *
from functools import partial
from canvas import *
from inputDlg import *


class WindowMixin(object):
    
    def menu(self, title, actions=None):
        menu = self.menuBar().addMenu(title)
        if actions:
            addActions(menu, actions)
        return menu

'''
    def toolbar(self, title, actions=None):
        toolbar = ToolBar(title)
        toolbar.setObjectName(u'%sToolBar' % title)
        toolbar.setOrientation(Qt.Vertical)
        toolbar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
        if actions:
            addActions(toolbar, actions)
        self.addToolBar(Qt.LeftToolBarArea, toolbar)
        return toolbar
    
'''   
 
class MainWindow(QMainWindow,WindowMixin):
    def __init__(self,parent=None):
        super(MainWindow,self).__init__(parent)
        
        top_widget=QWidget()
        self.setCentralWidget(top_widget)
        
        self.canvas = Canvas()
        
        topFiller = QWidget()
        topFiller.setMinimumSize (1200, 1200 )
        for ks in range(1, 60):
            label = QLabel( "<--------->", topFiller)
            label.move(ks*10, ks*10)
            
            
        scroll = QScrollArea()
        scroll.setWidget(topFiller)
        #scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn);
        scroll.setWidgetResizable(True)
        scroll.setAutoFillBackground(True)
        
        layout=QVBoxLayout()
        layout.addWidget(scroll)
        top_widget.setLayout(layout)
        
        
        
        
        self.openPic_action=newAction(self, "openfile", self.openfile, "Ctrl+E", "./icons/open.png","openfile")
        self.setPoint_action=newAction(self, "setPoint", self.setPoint, "Ctrl+E", "./icons/line.png","drawLine")
        self.drawPoint_action=newAction(self, "drawPoint", self.drawPoint, "Ctrl+E", "./icons/point.png","drawPoint")
        self.setRect_action=newAction(self, "setRect", self.setRect, "Ctrl+E", "./icons/circle.png","drawCircle")
        self.drawRect_action=newAction(self, "drawRect", self.drawRect, "Ctrl+E", "./icons/done.png","drawRect")
        
        opentoolbar=newToolBar("tool")
        opentoolbar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
        opentoolbar.setOrientation(Qt.Vertical)

        opentoolbar.addAction(self.openPic_action)
        opentoolbar.addAction(self.setPoint_action)
        opentoolbar.addAction(self.drawPoint_action)
        opentoolbar.addAction(self.setRect_action)
        opentoolbar.addAction(self.drawRect_action)
        
        self.addToolBar(Qt.LeftToolBarArea,opentoolbar)
        
        
        
    
    def openfile(self):    
        filename=QFileDialog.getOpenFileName(self,"sss","./","image file (*.png *.jpg)")
        print filename
        if filename != "":
            self.canvas.loadPixmap( filename )
            
    def setPoint(self):
        if self.canvas.picfile=='':
            QMessageBox.warning(self, "warning", "please load image first!!!")
            return 
        self.point_dlg=PointDlg()
        self.point_dlg.show()
        self.point_dlg.get_point.connect( self.setPoint_slot )
        
    def setPoint_slot (self,num1,num2):
        self.canvas.setPoint(num1, num2)
    
    def setRect(self):
        if self.canvas.picfile=='':
            QMessageBox.warning(self, "warning", "please load image first!!!")
            return 
        self.rect_dlg=RectangleDlg()  
        self.rect_dlg.show()
        self.rect_dlg.get_rec.connect( self.setRect_slot ) 
        
    def setRect_slot (self,num1,num2,num3,num4):
        self.canvas.setRect(num1, num2,num3, num4)
    
    def drawPoint(self):
        if self.canvas.picfile=='':
            QMessageBox.warning(self, "warning", "please load image first!!!")
            return 
        self.canvas.drawPoint()
        
    def drawRect(self):
        if self.canvas.picfile=='':
            QMessageBox.warning(self, "warning", "please load image first!!!")
            return 
        self.canvas.drawRect()
    
    def sizeHint(self ):
        return QSize(1000,1000)
    
        
        
app=QApplication(sys.argv)
win=MainWindow()
win.show()
app.exec_()

